package com.java2007.controller.backend;

import com.java2007.constant.ServletConstant;
import com.java2007.controller.BaseServlet;
import com.java2007.factory.BeanFactory;
import com.java2007.pojo.Food;
import com.java2007.pojo.FoodType;
import com.java2007.service.FoodService;
import com.java2007.service.FoodTypeService;
import com.java2007.util.FileUtil;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@WebServlet("/food")
@MultipartConfig
public class FoodController extends BaseServlet {

    FoodService foodService = (FoodService) BeanFactory.getBean("com.java2007.service.impl.FoodServiceImpl");

    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("com.java2007.service.impl.FoodTypeServiceImpl");

    /**
     * 查询全部
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findAll(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //调用相应方法
        List<Food> names = foodService.findAll();
        //结果存入对象
        request.setAttribute("names", names);
        //转发
        return ("forward:/backend/detail/food/food-list.jsp");
    }
    /**
     * 模糊查询全部
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String search(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求
        String foodName = (String) request.getSession().getAttribute("foodName");
        //条件存入域对象，作为回显值
        request.getSession().setAttribute("foodName", foodName);
        //调用相应方法
        List<Food> names = foodService.findByFoodName(foodName);
        //结果存入Session域对象
        request.setAttribute("names",names);
        System.out.println(names);
        //转发
        return ("forward:/backend/detail/food/food-list.jsp");
    }

    /**
     * 根据ID删除
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求
        String idStr = request.getParameter("foodId");
        //传入的ID不是int类型，转型
        int foodId = Integer.parseInt(idStr);
        request.getSession().setAttribute("idStr",idStr);
        //调用相应方法
        foodService.deleteByFoodId(foodId);
        //重定向
        return ("redirect:/food?method=search");
    }

    /**
     * 保存页面
     *  查询菜类别进行保存
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String saveUi(HttpServletRequest request,HttpServletResponse response)throws Exception{
        //查询菜类别转发到save
        List<FoodType> types = foodTypeService.findAll();
        if (null==types){
            return (ServletConstant.FORWARD+"/backend/detail/food/food-save.jsp");
        }
        request.getSession().setAttribute("types",types);
        return (ServletConstant.FORWARD+"/backend/detail/food/food-save.jsp");
    }

    /**
     * 添加
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台发来的请求，保存参数。
        String fileName = FileUtil.upload(request);
        Map<String, String[]> parameterMap = request.getParameterMap();
        Food food = new Food();
        BeanUtils.populate(food,parameterMap);
        food.setImage("/files/"+fileName);
        //调用相应方法
        foodService.saveFood(food);
        //重定向
        return (ServletConstant.REDIRECT+"/food?method=search");
    }

    /**
     * 通过ID查询
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String byId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求
        String strId = request.getParameter("foodId");
//        String strId = (String) request.getSession().getAttribute("foodId");
        //传入的ID不是int类型，转型
        int foodId = Integer.parseInt(strId);
        //调用相应方法
        Food food = foodService.findByFoodId(foodId);
        //存入域对象
        request.getSession().setAttribute("food", food);
        //转发
        return ("forward:/backend/detail/food/food-update.jsp");
    }

    /**
     * 更新
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台发来的请求，保存参数。
        String fileName = FileUtil.upload(request);
        Map<String, String[]> parameterMap = request.getParameterMap();
        Food food = new Food();
        BeanUtils.populate(food,parameterMap);
        food.setImage("/files/"+fileName);
        System.out.println(food);
        //调用相应方法
        foodService.updata(food);
        //重定向
        return ("forward:/food?method=findAll");

    }
}
